package com.service.impl;

import com.domain.tab_user;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
import org.springframework.data.relational.core.query.Criteria;
import org.springframework.data.relational.core.query.Query;
import org.springframework.data.relational.core.query.Update;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * @Author: 许磊磊
 * @Date: 2022/10/18 19:49
 * @Version 1.0
 */
@Service
public class UserServiceImpl implements UserService {

    private R2dbcEntityTemplate r2dbcEntityTemplate;

    @Autowired
    public void setR2dbcEntityTemplate(R2dbcEntityTemplate r2dbcEntityTemplate){
        this.r2dbcEntityTemplate=r2dbcEntityTemplate;
    }


    @Override
    public Flux<tab_user> findAll() {
        return  r2dbcEntityTemplate.select(tab_user.class).all();
    }

    @Override
    public Mono<tab_user> findOne(String id) {
        return r2dbcEntityTemplate
                .selectOne(Query.query(Criteria.where("uid").is(id)),tab_user.class);
    }

    @Override
    public Mono<Integer> updateOne(tab_user user) {
        return r2dbcEntityTemplate.update(tab_user.class)
                .inTable("tab_user").matching(Query.query(Criteria.where("uid").is(user.getUid())))
                .apply(Update.update("uname",user.getUname()));
    }

    @Override
    public Mono<Integer> deleteOne(String uid) {
        return  r2dbcEntityTemplate.delete(tab_user.class)
                .matching(Query.query(Criteria.where("uid").is(uid))).all();
    }

    @Override
    public Mono<Integer> insertOne(tab_user user) {
        return r2dbcEntityTemplate.insert(tab_user.class)
                .using(user).map(user1 -> 1);
    }


}
